package benny_driving_ser.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;


import benny_driving_ser.ObjectBean.BonusInfo;
import benny_driving_ser.ObjectBean.RandomBonusBean;
import benny_driving_ser.dao.DriRandomBonusDao;
import benny_driving_ser.util.DBUtil;
import benny_driving_ser.util.FormatUtil;

public class DriRandomBonusImpl implements DriRandomBonusDao{
	
	
	public static boolean makeBonusDao(int orderid,Connection conn) {
		
		Random random = new Random();
		String sql_query_order = "select id,dri_id,state from t_djpd where id = "+orderid;
		String sql_query_times = "select count(*) count from t_bonus_rule1";
		String sql_query_ids = "select count(*) count from t_bonus_rule1 where bonus_used = 0";
		String sql_update_ids = "update t_bonus_rule1 set bonus_used = 0";
		String sql_query_id = "select id,bonus_cost,bonus_used from t_bonus_rule1 where bonus_used = 0";
		String sql_query_byid = "select id,bonus_cost,bonus_used from t_bonus_rule1 where bonus_used = 0 and id = ?";
		String sql_update_rule = "update t_bonus_rule1 set bonus_used = 1 where id = ?";
		String sql_update_order = "update t_djpd set bonus_id = ?,bonus_cost = ? where id = ?";
		String sql_insert = "insert into t_bonus_info(bonus_cost,orderid,time,driid) value(?,?,?,?)";
		
		
		int dri_id = 0;
		int maxtimes = 0;
		int arraySize = 0;
		int[] ids;
		try{
			conn.setAutoCommit(false);
			PreparedStatement pst_query_times = conn.prepareStatement(sql_query_times);
			ResultSet rs_query_times = pst_query_times.executeQuery();
			if(rs_query_times.next()){
				maxtimes = rs_query_times.getInt("count");
			}
			rs_query_times.close();
			pst_query_times.close();
			
			PreparedStatement pst_query_ids = conn.prepareStatement(sql_query_ids);
			ResultSet rs_query_ids = pst_query_ids.executeQuery();
			while(rs_query_ids.next()){
				arraySize = rs_query_ids.getInt("count");
				
				
				if(arraySize>0){
					ids = new int[arraySize];
					PreparedStatement pst_query_id = conn.prepareStatement(sql_query_id);
					ResultSet rs_query_id = pst_query_id.executeQuery();
					int index = 0;
					while(rs_query_id.next()){
						ids[index] = rs_query_id.getInt("id");
						index ++;
					}
					int randomint = random.nextInt(ids.length);
					int bonusid = ids[randomint];
					PreparedStatement pst_query_byid = conn.prepareStatement(sql_query_byid);
					pst_query_byid.setInt(1, bonusid);
					ResultSet rs_query_byid = pst_query_byid.executeQuery();
					if(rs_query_byid.next()){
						int bonus_cost = rs_query_byid.getInt("bonus_cost");
						
						PreparedStatement pst_update_rule = conn.prepareStatement(sql_update_rule);
						pst_update_rule.setInt(1, bonusid);
						pst_update_rule.executeUpdate();
						
						pst_update_rule.close();
						
						PreparedStatement pst_query_order = conn.prepareStatement(sql_query_order);
						ResultSet rs_query_order = pst_query_order.executeQuery();
						while(rs_query_order.next()){
							dri_id = rs_query_order.getInt("dri_id");
						}
						rs_query_order.close();
						pst_query_order.close();
						PreparedStatement pst_insert = conn.prepareStatement(sql_insert);
						pst_insert.setInt(1, bonus_cost);
						pst_insert.setInt(2, orderid);
						pst_insert.setString(3, FormatUtil.getTimeFormat(System.currentTimeMillis()));
						pst_insert.setInt(4, dri_id);
						pst_insert.executeUpdate();
						pst_insert.close();
						PreparedStatement pst_update_order = conn.prepareStatement(sql_update_order);
						pst_update_order.setInt(1, bonusid);
						pst_update_order.setInt(2, bonus_cost);
						pst_update_order.setInt(3, orderid);
						pst_update_order.executeUpdate();
						pst_update_order.close();
						System.out.println("本次抢单中奖金额为:"+bonus_cost+"元!");
						
						return true;
					}
					rs_query_byid.close();
					pst_query_byid.close();
					
				}
			}
			
			PreparedStatement pst_update_ids = conn.prepareStatement(sql_update_ids);
			pst_update_ids.executeUpdate();
			pst_update_ids.close();
			makeBonusDao(orderid,conn);
			rs_query_ids.close();
			pst_query_ids.close();
			
		}catch(SQLException e){
			e.printStackTrace();
			try {
				conn.rollback();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
			System.out.println("SQL语句异常.");
			return false;
		}
		return true;
	}
	
	
	public List<BonusInfo> loadBonusInfo(){
		List<BonusInfo> bonusInfos = null;
		String  sql_quer = "";
		return bonusInfos;
	}
	
	
//	public static void main(String[] args) {
//		System.out.println(makeBonusDao(12750,DBUtil.getInstance().openConnection()));
//	}
	
}
